/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Threads;

import DAO.TinTucDAO;
import DAO.XpathDAO;
import DTO.TinTucDTO;
import DTO.XpathDTO;
import GUI.Test;
import GUI.pnNews;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;

/**
 *
 * @author QuangHau
 */
public class getNews extends Thread {

    JTree treeWebsite;
    JScrollPane pnNews;
    JLabel lbstatus;
    JButton btnLayTin;

    public getNews(Object[] args) {
        treeWebsite = (JTree) args[0];
        pnNews = (JScrollPane) args[1];
        lbstatus = (JLabel) args[2];
        btnLayTin = (JButton) args[3];
        btnLayTin.setEnabled(false);
    }

    @Override
    public void run() {
        writeLbStatus wt = null;
        if (!Comvar.writeStatus) {
            Comvar.writeStatus = true;
            wt = new writeLbStatus(lbstatus, "Đang lấy tin");
            wt.start();
        }
        ArrayList<XpathDTO> listxpath = XpathDAO.selectXpathAll();
        if (listxpath != null & listxpath.size() > 0) {
            JPanel pn = new JPanel();
            pn.setLayout(new BoxLayout(pn, BoxLayout.Y_AXIS));
            ArrayList<TinTucDTO> listTinTucAll = new ArrayList<TinTucDTO>();
            for (int i = 0; i < listxpath.size(); i++) {
                XpathDTO item = listxpath.get(i);
                ArrayList<TinTucDTO> listTinTuc = new ArrayList<TinTucDTO>();
                try {
                    listTinTuc = item.getNewsAllPage();
                    if (listTinTuc != null && listTinTuc.size() > 0) {
                        for (int j = listTinTuc.size() - 1; j >= 0; j--) {
                            if (listTinTuc.get(j).getLinkChuDe().indexOf("vietnamnet.vn") != -1) {
                                listTinTuc.get(j).setLink(listTinTuc.get(j).getLinkChuDe().substring(0, listTinTuc.get(j).getLinkChuDe().indexOf("vietnamnet.vn"))
                                        + "vietnamnet.vn" + listTinTuc.get(j).getLink());
                            } else if (listTinTuc.get(j).getLinkChuDe().indexOf("nld.com.vn") != -1) {
                                listTinTuc.get(j).setLink(listTinTuc.get(j).getLinkChuDe().substring(0, listTinTuc.get(j).getLinkChuDe().indexOf("nld.com.vn"))
                                        + "nld.com.vn" + listTinTuc.get(j).getLink());
                            }
                            TinTucDAO.InsertTinTuc(listTinTuc.get(j));
                            TinTucDTO tintuc = TinTucDAO.SelectTinTuc(TinTucDAO.maxId());
                            pn.add(new pnNews(tintuc, 1));
                            listTinTucAll.add(tintuc);
                            downLoadImage dli = new downLoadImage(listTinTuc.get(j).getHinhAnh(), tintuc.getMaTinTuc());
                            dli.start();
                            System.out.println("insert " + listTinTuc.get(j).getTieuDe());
                        }
                    }
                } catch (Exception ex) {
                    //Logger.getLogger(getNews.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(null, "Không thể lấy tin.", "Lỗi", JOptionPane.ERROR_MESSAGE);
                }

            }
            loadTreeWebsite t = new loadTreeWebsite(new Object[]{treeWebsite, pnNews, lbstatus, btnLayTin, -1, -1});
            t.start();
            if (listTinTucAll.size() > 0) {
                Test test = new Test(null, true, listTinTucAll, 1);
                GUI.GUI.ShowCenterScreen(test);
                test.setVisible(true);
            } else {
                JOptionPane.showMessageDialog(null, "Không có tin tức mới", "Thông tin", JOptionPane.INFORMATION_MESSAGE);
            }
            try {
                t.join();
            } catch (InterruptedException ex) {
                Logger.getLogger(loadTreeWebsite.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        if (wt != null) {
            wt.stopWrite();
            Comvar.writeStatus = false;
        }
        btnLayTin.setEnabled(true);
    }
}
